<template>
  <div>
    <el-dialog :title="info.title" :visible.sync="info.show">
      <el-form :model="form">
        <el-form-item label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option v-for="item in role" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="blue"
            inactive-color="grey"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm" v-if="info.isAdd">添加</el-button>
        <el-button type="primary" @click="update" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert } from '../../../utils/alert';
import { addManager, oneManager, updateManager } from '../../../utils/request';
export default {
  props: ["info"],
  computed: {
    ...mapGetters({
      "role": "role/role",
    }),
  },
  methods: {
    ...mapActions({
      "requestRole": "role/roleActions",
      "requestManager":"manager/managerActions",
      "requestCount":"manager/countActions"
    }),
    cancel() {
      this.info.show = false;
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    confirm(){
      addManager(this.form).then(res=>{
        successAlert(res.data.msg)
        this.cancel()
        this.requestManager()
        this.requestCount()
      })
      // console.log(this.form)
    },
    getDetail(uid){
          oneManager({uid}).then(res=>{
             this.form = res.data.list
             this.form.password=''
          })
    },
    update(){
      // console.log(this.form)
      updateManager(this.form).then(res=>{
        successAlert(res.data.msg)
        this.cancel()
        this.requestManager()
      })
    }
  },
  mounted() {
    this.requestRole();
  },
  data() {
    return {
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
};
</script>

<style scoped>
 .el-input{
     width: 220px;
   }
</style>